﻿using System.Collections;
using DVDTek_Data_Object.DataObject;

namespace DVDTek_Data_Object.Collection
{
    public class MovieCollection
    {
        #region Members
        private ArrayList movies = new ArrayList();
        
        public int Count
        {
            get { return this.movies.Count; }
        }

        public MovieDataObject this[int i]
        {
            get
            {
                return (MovieDataObject)this.movies[i];
            }
            set
            {
                if (value == null)
                    RemoveAt(i);
                else
                    this.movies[i] = value;
            }
        }
        #endregion

        #region Constructors
        public MovieCollection()
        {
        }
        #endregion

        #region Functions
        public void Add(MovieDataObject item)
        {
            this.movies.Add(item);
        }

        public void Clear()
        {
            this.movies.Clear();
        }

        public bool Contains(MovieDataObject item)
        {
            return this.movies.Contains(item);
        }

        #region IndexOf
        public int IndexOf(MovieDataObject item)
        {
            return this.movies.IndexOf(item);
        }

        public int IndexOf(MovieDataObject item, int startIndex)
        {
            return this.movies.IndexOf(item, startIndex);
        }

        public int IndexOf(MovieDataObject item, int startIndex, int count)
        {
            return this.movies.IndexOf(item, startIndex, count);
        }
        #endregion

        public void Insert(int index, MovieDataObject value)
        {
            this.movies.Insert(index, value);
        }

        #region LastIndexOf
        public int LastIndexOf(MovieDataObject value)
        {
            return this.movies.LastIndexOf(value);
        }

        public int LastIndexOf(MovieDataObject value, int startIndex)
        {
            return this.movies.LastIndexOf(value, startIndex);
        }

        public int LastIndexOf(MovieDataObject value, int startIndex, int count)
        {
            return this.movies.LastIndexOf(value, startIndex, count);
        }
        #endregion

        #region Remove
        public bool Remove(MovieDataObject item)
        {
            int index = this.movies.IndexOf(item);
            if (index >= 0)
            {
                this.movies.RemoveAt(index);
                return true;
            }
            else
                return false;
        }

        public void RemoveAt(int i)
        {
            this.movies.RemoveAt(i);
        }

        public void RemoveRange(int index, int count)
        {
            this.movies.RemoveRange(index, count);
        }
        #endregion

        #region Reverse
        public void Reverse()
        {
            this.movies.Reverse();
        }

        public void Reverse(int index, int count)
        {
            this.movies.Reverse(index, count);
        }
        #endregion

        #region Sort
        public void SortByTitle(bool ascending)
        {
            for (int i = 0; i < Count - 1; i++)
            {
                int index = i;
                for (int j = i + 1; j < Count; j++)
                {
                    if (string.Compare(this[index].Title, this[j].Title) > 0)
                        index = j;
                }
                if (index != i)
                {
                    MovieDataObject movie = this[index];
                    this[index] = this[i];
                    this[i] = movie;
                }
            }

            if (!ascending)
                Reverse();
        }

        public void SortByOrigin(bool ascending)
        {
            for (int i = 0; i < Count - 1; i++)
            {
                int index = i;
                for (int j = i + 1; j < Count; j++)
                {
                    if (string.Compare(this[index].Origin, this[j].Origin) > 0)
                        index = j;
                }
                if (index != i)
                {
                    MovieDataObject movie = this[index];
                    this[index] = this[i];
                    this[i] = movie;
                }
            }

            if (!ascending)
                Reverse();
        }

        public void SortByYear(bool ascending)
        {
            for (int i = 0; i < Count - 1; i++)
            {
                int index = i;
                for (int j = i + 1; j < Count; j++)
                {
                    if (this[index].Year > this[j].Year)
                        index = j;
                }
                if (index != i)
                {
                    MovieDataObject movie = this[index];
                    this[index] = this[i];
                    this[i] = movie;
                }
            }

            if (!ascending)
                Reverse();
        }

        public void SortBySeen(bool ascending)
        {
            int iteration = Count;
            for (int i = 0; i < iteration; i++)
            {
                MovieDataObject movie = this[i];
                if (movie.Seen)
                {
                    this.RemoveAt(i);
                    this.Add(movie);
                    --iteration;
                }
            }

            if (!ascending)
                Reverse();
        }

        public void SortByAcquired(bool ascending)
        {
            int iteration = Count;
            for (int i = 0; i < iteration; i++)
            {
                MovieDataObject movie = this[i];
                if (movie.Acquired)
                {
                    this.RemoveAt(i);
                    this.Add(movie);
                    --iteration;
                }
            }

            if (!ascending)
                Reverse();
        }

        public void SortByAvailable(bool ascending)
        {
            int iteration = Count;
            for (int i = 0; i < iteration; i++)
            {
                MovieDataObject movie = this[i];
                if (movie.Available)
                {
                    this.RemoveAt(i);
                    this.Add(movie);
                    --iteration;
                }
            }

            if (!ascending)
                Reverse();
        }

        public void SortBySupport(bool ascending)
        {
            for (int i = 0; i < Count - 1; i++)
            {
                int index = i;
                for (int j = i + 1; j < Count; j++)
                {
                    if (string.Compare(this[index].Support, this[j].Support) > 0)
                        index = j;
                }
                if (index != i)
                {
                    MovieDataObject movie = this[index];
                    this[index] = this[i];
                    this[i] = movie;
                }
            }

            if (!ascending)
                Reverse();
        }
        #endregion

        public MovieDataObject[] ToArray()
        {
            MovieDataObject[] array = new MovieDataObject[Count];
            for (int i = 0; i < Count; i++)
                array[i] = this[i];
            return array;
        }

        public void TrimToSize()
        {
            this.movies.TrimToSize();
        }
        #endregion
    }
}
